//
//  TXFUserAccountTools.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/6/26.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit

class TXFUserAccountTools: NSObject {
//设置单例为了更好的在外界使用
    var userAccount : TXFUserAccount?
    
     //根据数据判断是否登录过
    
    var isLogin: Bool {
    
    
        return accessToken != nil
    
    
    }
    //是否有访问令牌:
    var accessToken: String? {
        // 代表没有登录
        if userAccount?.access_token == nil {
            return nil
        }else {
            // 如果expires_Date 于当前时间比较是降序 代表没有过去
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return userAccount?.access_token
            }else {
                return nil
            }
        }
    }
    
    
    
    
    static let sharedTools : TXFUserAccountTools =  TXFUserAccountTools()
    
     override init() {
        
        super.init()
//获取数据:
        userAccount =  getUserAccount()
        
        
    }
    //归档,解档//
    
    //获取归档解档的地址
    //保存数据
func saveUserAccount(userAccount: TXFUserAccount){
    
    
        self.userAccount = userAccount
        //拼接保存的路径:
        
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("123.plist")
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
        
    
    }
    //提取数据:
    
    func getUserAccount()->TXFUserAccount?{
    
      let path = ( NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("123.plist")
    
    let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path)as? TXFUserAccount
        
       
    return result
    }
  
}
